<?php

class Home_Controller extends Base_Controller {


	public function action_index()
	{
		

		$code = Input::get('code');
		if($code!=null){
			$res = Api::get_wyaccess_token($code);
			$wyaccess_token = $res['wyaccess_token'];
			$openid = $res['openid'];
            Session::put('openid',$openid);
			$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$wyaccess_token.'&openid='.$openid.'&lang=zh_CN';//拉取用户信息的url地址
			$res =Api::http_curl($url);
			return View::make('home.index',array('res'=>$res));
        }else{
        	return View::make('home.index');
        }
		
	}



	//获取微信用户基本信息（公众号的菜单的url地址）
    public function action_getuserinfo()
    {

	   	$appid  = API::get_appid();
	   	//$url = 'http://persontest.cc/home/index';
	   	$redirect_uri = urlencode("http://persontest.wuliangkeji.cn/home/");//网页授权后跳转的地址
	   	$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect';
	   	header('location:'.$url);

    }


    public function action_dopost()
    {
    	//获取用户基本信息,并插入数据库对应的表中
    	$openid = Input::get('openid');  
    	$nickname = Input::get('nickname');
    	$sex1= Input::get('sex');
    	$sex = ($sex1==1)? '男' : '女';
    	$province = Input::get('province');
    	$city = Input::get('city');
    	$headimgurl = Input::get('headimgurl');
    	$realname = Input::get('username');
    	$phone = Input::get('phone');
    	$birthday = Input::get('birthday');
    	$subscribe_time = time();
    	$str1 = DB::table('member')->where('openid','=',$openid)->first();
    	if($str1){
    		$str = DB::table('member')->where('openid','=',$openid)->update(array('openid'=>$openid,'nickname'=>$nickname,'sex'=>$sex,'province'=>$province,'city'=>$city,'headimgurl'=>$headimgurl,'phone'=>$phone,'birthday'=>$birthday,'subscribe_time'=>$subscribe_time,'realname'=>$realname));
    	}else{
    		$str = DB::table('member')->insert(array('openid'=>$openid,'nickname'=>$nickname,'sex'=>$sex,'province'=>$province,'city'=>$city,'headimgurl'=>$headimgurl,'phone'=>$phone,'birthday'=>$birthday,'subscribe_time'=>$subscribe_time,'realname'=>$realname));
    	}
    	$res1 = DB::table('member')->where('openid','=',$openid)->first();
    	$id = $res1['id'];
    	
    	//接下去就根据生日来算出三组数据;插入到对应的表中
    	$res = Api::creatarrfactor($birthday);
    	$first = $res['first'];
    	$second = $res['second'];
    	$third = $res['third'];
        $factornumber = $first."-".$second."-".$third;
    	$res2 = DB::table('member_factor')->where('mid','=',$id)->first();
    	if($res2){
    		$res3 = DB::table('member_factor')->where('mid','=',$id)->update(array('factornumber'=>$factornumber));
    	}else{
    		$res3 = DB::table('member_factor')->insert(array('mid'=>$id,'factornumber'=>$factornumber));
    	}


        $order_number = Api::creatorder();
        $res4 = DB::table('member_order')->where('mid','=',$id)->first();
        if($res4==null){
            $res5 = DB::table('product_price')->where('id','=',1)->first();
            $price = $res5['present_price'];
            $creattime = time();
           
            $res5 = DB::table('member_order')->insert(array('mid'=>$id,'order_number'=>$order_number,'price'=>$price,'creattime'=>$creattime));  
        }
        return Redirect::to('/home/pay');

    }


    //显示支付界面
    public function action_pay()
    {

        $res = DB::table('product_price')->where('id','=',1)->first();
        // 需要支付的价格
        $price = $res['present_price'];
        // 从数据库中获取用户的订单号;
        $openid = Session::get('openid');
        $appid = Api::get_appid();
        $noncestr = Api::createNonceStr();
        $str = DB::table('member')->where('openid','=',$openid)->first();
        $mid = $str['id'];
        $str1 = DB::table('member_order')->where('mid','=',$mid)->first();
        $order_number = $str1['order_number'];
        //获取用户的openid;
        $tools =  wxpay::getJsApiPay();
        $openId = $tools->GetOpenid();
        //②、统一下单
        $input = new WxPayUnifiedOrder();         
        //$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("test");
        $input->SetNotify_url("http://persontest.wuliangkeji.cn/home/setpay"); //后台接口地址，用户支付完成，微信会通知你支付结果
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $input->SetBody("test");
        $input->SetAttach("test");
        $input->SetOut_trade_no($order_number);
        $input->SetTotal_fee($price);
        $order = WxPayApi::unifiedOrder($input);
        var_dump($order);  
        exit;
        $jsApiParameters = $tools->GetJsApiParameters($order);
        return View::make('home.pay',array('jsApiParameters'=>$jsApiParameters,'res'=>$str));
    }



    //支付成功后调用的显示页面
    public function action_showanaly()
    {
        $openid = Session::get('openid');
        $str =  DB::table('member')->where('openid','=',$openid)->first();
        $birthday = $str['birthday'];
        $res = Api::creatarrfactor($birthday);
        $first = $res['first'];
        $second = $res['second']; //3位数的因素
        $third = $res['third'];
        $res4 = DB::table('factor')->where('lien','=',1)->where('lien_number','=',1)->first();
        $first_explanination = $res4['explanination'];
        $res4 = DB::table('factor')->where('lien','=',2)->where('lien_number','=',1)->first();
        $second_explanination = $res4['explanination'];
        $res4 = DB::table('factor')->where('lien','=',3)->where('lien_number','=',111)->first();
        $third_explanination = $res4['explanination'];
        //为显示分析页面微信分享接口做准备;
        $url =  Api::get_url();
        $arr = Api::creat_signaturedata($url);
        return View::make('home.analysisresult',array('first_explanination'=>$first_explanination,'second_explanination'=>$second_explanination,'third_explanination'=>$third_explanination,'arr'=>$arr));

    }

    public function action_setpay()
    {

        return "SUCCESS";
    }



    public function action_ceshipay()
    {


        return View::make('home.ceshipay');
    }













   
  

























}